#include <browser.h>

#include <QIcon>

/** Cria browser e adiciona os elementos(BrowserArea, AddressBar e BottomBar),
 * cria historico escondido(BrowserHistory) e conecta os seguintes sinais e slots:
 *
 * AddressBar::load => BrowserArea::load
 * AddressBar::reload => BrowserArea::reload
 * AddressBar::forward => BrowserArea::forward
 * AddressBar::back => BrowserArea::back
 * AddressBar::stop => BrowserArea::stop
 * BrowserArea::urlChanged => AddressBar::setAddress
 * BrowserArea::titleChanged => Browser::titleChanged
 * BrowserArea::statusBarMessage => BottomBar::setStatusMessage
 * BrowserArea::urlVisited => BrowserHistory::addHistoryEntry
 * BottomBar::newTab => BrowserArea::newTab
 * BottomBar::closeTab => BrowserArea::closeTab
 * BottomBar::showHistory => Browser::showHistory
 * BottomBar::showConf => Browser::showConf
 * BrowserHistory::historyEntrySelected => BrowserArea::load
 */
Browser::Browser(QWidget *parent) : QWidget(parent)
{
    /* seta titulo inicial */
    setWindowTitle("QT Browser");

    /* Cria e seta layout do widget */
    layout = new QVBoxLayout(this);

    /* Cria objetos do layout */
    addressBar = new AddressBar();
    browserArea = new BrowserArea();
    bottomBar = new BottomBar();

    /* Adiciona objetos ao layout */
    layout->addWidget(addressBar);
    layout->addWidget(browserArea);
    layout->addWidget(bottomBar);

    /* Cria sub-janelas de historico e configuração */
    browserHistory = new BrowserHistory();
    browserConf = new BrowserConf();

    /* Seta icones da aplicação */
    setWindowIcon(QIcon(":/icons/browser.png"));
    browserHistory->setWindowIcon(QIcon(":/icons/history.png"));
    browserConf->setWindowIcon(QIcon(":/icons/conf.png"));

    /* Conecta sinais aos slots */

    /* Acoes da barra de endereços à janela de navegação */
    connect(addressBar, SIGNAL(load(const QUrl&)), browserArea, SLOT(load(const QUrl&)));
    connect(addressBar, SIGNAL(reload()), browserArea, SLOT(reload()));
    connect(addressBar, SIGNAL(forward()), browserArea, SLOT(forward()));
    connect(addressBar, SIGNAL(back()), browserArea, SLOT(back()));
    connect(addressBar, SIGNAL(stop()), browserArea, SLOT(stop()));

    /* Ações da janela de navegação ao resto da aplicação */
    connect(browserArea, SIGNAL(urlChanged(const QUrl&)), addressBar, SLOT(setAddress(const QUrl&)));
    connect(browserArea, SIGNAL(titleChanged(const QString&)), this, SLOT(titleChanged(const QString&)));
    connect(browserArea, SIGNAL(statusBarMessage(const QString&)), bottomBar, SLOT(setStatusMessage(const QString&)));
    connect(browserArea, SIGNAL(urlVisited(const QUrl&)), browserHistory, SLOT(addHistoryEntry(const QUrl&)));

    /* Açoes da barra de status à janela de navegação e para lançar as sub-janelas */
    connect(bottomBar, SIGNAL(newTab()), browserArea, SLOT(newTab()));
    connect(bottomBar, SIGNAL(showHistory()), this, SLOT(showHistory()));
    connect(bottomBar, SIGNAL(showConf()), this, SLOT(showConf()));
    connect(bottomBar, SIGNAL(showPages()), browserArea, SLOT(chooseActivePage()));
    connect(bottomBar, SIGNAL(closeTab()), browserArea, SLOT(closeTab()));

    /* Carregar item do histórico na janela de navegação */
    connect(browserHistory, SIGNAL(historyEntrySelected(const QUrl&)), browserArea, SLOT(load(const QUrl&)));
}

Browser::~Browser()
{
    /* Libera memória */
    delete addressBar;
    delete browserArea;
    delete bottomBar;
    delete browserHistory;
    delete browserConf;
}


/** Muda o titulo da janela */
void Browser::titleChanged(const QString &text)
{
    setWindowTitle(text);
}

/** Mostra(show) o widget BrowserHistory */
void Browser::showHistory()
{
    browserHistory->show();
}

/** Cria e mostra(show) o widget BrowserConf */
void Browser::showConf()
{
    browserConf->show();
}

void Browser::resizeEvent ( QResizeEvent * event )
{
    Q_UNUSED( event );

    browserArea->novoTamanho();
}
